import subprocess
from functools import partial
subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")
import json
import execjs
import requests


class SXCG:
    def __init__(self):
        self.url = 'https://shanxisheng-zfcgdzmcgov.cn/gpfa-bpoc/api/notice/kc/v1/ignore/pagingKcAgreementNotice'
        self.headers = {
            "accept": "application/json, text/plain, */*",
            "accept-encoding": "gzip, deflate, br, zstd",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "no-cache",
            "content-length": "0",
            "content-type": "application/json",
            "cookie": "webapp=1; siteCode=sxzfcg; siteGuid=6883226032309207040; regioncode=140001000; regionguid=140001; theme=default; regionpguid=232; rootregionguid=232; isOpenArea=true; regionname=%E7%9C%81%E6%9C%AC%E7%BA%A7",
            "nsssjss": "",
            "origin": "https://shanxisheng-zfcgdzmcgov.cn",
            "pragma": "no-cache",
            "priority": "u=1, i",
            "referer": "https://shanxisheng-zfcgdzmcgov.cn/gpmall-main-web/basic/sxNotice",
            "regioncode": "140001000",
            "regionguid": "140001",
            "sec-ch-ua": "\"Chromium\";v=\"136\", \"Microsoft Edge\";v=\"136\", \"Not.A/Brand\";v=\"99\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "sitecode": "sxzfcg",
            "siteguid": "6883226032309207040",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0",
            "webapp": "1"
        }
        self.data = {
            "noticeTypeList": ["gpfa_notice", "gpfa_notice_modify", "gpfa_notice_resume", "gpfa_notice_pause"],
            "pageNum": 2,
            "pageSize": 10,
            "regionCode": "140000000",
            "regionGuid": "232"
        }

    # 获取加密字段
    def get_nsssjss(self):
        with open('山西政府采购.js', 'r', encoding='utf-8') as f:
            js = f.read()
        js = execjs.compile(js)
        code = js.call('get_data')
        self.headers['nsssjss'] = code[0]

    # 获取数据
    def get_data(self, page):
        # 换页
        self.data['pageNum'] = page
        # 请求参数为字符串，：之间没有空格
        data = json.dumps(self.data, separators=(',',':'))
        try:
            response = requests.post(self.url, headers=self.headers, data=data)
            response.raise_for_status()
            response.encoding = response.apparent_encoding
            return response.json()['data']['data']
        except Exception as e:
            print(e)

    # 解析数据
    def save_data(self, data):
        for info in data:
            title = info['title']
            url = info['path']
            name = info['projectName']
            print(title, name, url)

    def main(self):
        for page in range(1, 6):
            self.get_nsssjss()
            data = self.get_data(page)
            self.save_data(data)


if __name__ == '__main__':
    sxcg = SXCG()
    sxcg.main()